/*
 * Copyright (C) 2024 ~ 2025 Deepin Technology Co., Ltd.
 *
 * Author:     zhangbingyi <zhangbingyi@uniontech.com>
 *
 * Maintainer: zhangbingyi <zhangbingyi@uniontech.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "usbinfolabel.h"
#include "common.h"

#include <QVBoxLayout>

UsbInfoLabel::UsbInfoLabel(const QString &text, QWidget *parent)
    : QLabel (text, parent)
    , m_originText(text)
{
    setWordWrap(true);
    setAlignment(Qt::AlignLeft);
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    installEventFilter(this);
}

UsbInfoLabel::~UsbInfoLabel()
{
}

void UsbInfoLabel::setOriginText(QString text)
{
    m_originText = text;
}

void UsbInfoLabel::resizeEvent(QResizeEvent *event)
{
    Q_UNUSED(event);
    QString displayText = Utils::wordwrapText(QFontMetrics(font()), m_originText, width());
    setText(displayText);
}

bool UsbInfoLabel::eventFilter(QObject *obj, QEvent *event)
{
    if (this == obj && QEvent::Type::FontChange == event->type()) {
        QString displayText = Utils::wordwrapText(QFontMetrics(font()), m_originText, width());
        setText(displayText);
    }
    return QWidget::eventFilter(obj, event); //其他事件按照原来处理
}
